/*
 *  enotes - an EFL task list manager
 *  Copyright (C) 2008 Igor Paolo Scabini
 *
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  contact the author on line at furester@http://xenos.altervista.org/
 *
 */
#include "enotes.h"
#include "util/storage.h"
#include "view/view.h"

#include <string.h>
#include <getopt.h>

/* return string representing current datetime */
char *
get_time_str (time_t tm)
{
   char *str;
   time_t lt;

   // The time function returns the current time as a value of type time_t.
   lt = time (NULL);

   // add an arbitrary time to current date in seconds
   // for example 86400 for next day
   lt = lt + tm;

   str = ctime(&lt);
   str[strlen(str) - 1] = '\0';

   return str;
}

/*
 * Funzione che prende una sottostringa partendo da un'indice
 * base per un numero di caratteri pari a dimensione
 *
 * Function that takes a substring from a
 * basis for a number of characters equal size
 */
void substring(char* dest, char *source, int i_base, int i_dim)
{ // substring() - OPEN

   int i = 0;

   for (i = i_base; i < i_dim + 1; i++)
   { // FOR - OPEN

     dest[i - i_base] = source[i];

   } // FOR - CLOSE

   dest[i]='\0';

   return;

} // substring() - CLOSE


/* Prints usage information for this program to
 * STREAM (typically stdout or stderr), and exit
 * the program with EXIT_CODE. Does not
 * return.
 */
void print_usage (FILE* stream, int exit_code)
{
   fprintf (stream, "Usage: %s [options]\n", program_name);
   fprintf (stream,
      " -h, --help\t"
      "Display this usage information.\n"
      " -v, --version\t"
      "Display the version of the program.\n");
   fprintf (stream, "For bug reporting instruction, please visit:\n"
		   "<http://xenos.altervista.org/>.\n");
   exit (exit_code);
}

/* Prints version information for this program to STREAM, and
 * exit the program with EXIT_CODE. Does not return. */
void
print_version(FILE* stream, int exit_code)
{
   fprintf (stream, " ..::: " PACKAGE " - " VERSION " :::..\n");

   fprintf (stream, "\nCopyright (C) 2008 Igor Paolo Scabini\n");
   fprintf (stream, "This is free software; see the source for copying conditions. There is NO"
		   " warranty; not even for MERCHANTABILITY or FITNESS FOR PARTICULAR PURPOSE\n");
   fprintf (stream, "\nreport bug to:\n");
   fprintf (stream, "\tfurester@xenos.altervista.org\n");

   exit (exit_code);
}

int
main(int argc, char **argv, char **arge)
{

   char path[256];
   printf("DEBUG: package name is '%s' ;\n", PACKAGE);

   if (!getenv("HOME"))
      sprintf(path, "%s/.%s/", strdup(getenv("CWD")), PACKAGE);
   else
	  sprintf(path, "%s/.%s/", strdup(getenv("HOME")), PACKAGE);

   pathname = path;
   printf("DEBUG: storage in %s ;\n", pathname);
   dbname = calloc (1,256);
   sprintf ((char *)dbname, "%senotes.db", pathname);
   catname = calloc(1, 256);
   sprintf ((char *)catname, "todo");

   /* Init Stuff */
   // TODO - spostare in view
   /* init Elementary (all Elementary calls begin with elm_ and all data
    * types, enums and macros will be Elm_ and ELM_ etc.) */
   elm_init(argc, argv);
   ecore_init();
   evas_init();
   ecore_evas_init();
   edje_init();

   /* options management */
   int next_option;
   /* a string listing valid short options letters. */
   const char* const short_options = "hv";
   /* an array describing valid long options. */
   const struct option long_options[] = {
	   { "help",	0, NULL, 'h'},
	   { "version",	0, NULL, 'v'},
	   { NULL,	0, NULL, 0}	/* required */
   };

   /* remember the name of the program to incorporate in messages.
    * The name is stored in argv[0]. */
   program_name = argv[0];

   do {
      next_option = getopt_long (argc, argv, short_options, long_options, NULL);

      switch (next_option)
      {
         case 'h': /* -h or --help */
		 print_usage (stdout, 0);
	 case 'v': /* -v or --version */
		 print_version (stdout, 0);
		 break;
	 case '?': /* the user specified an invalid option */
		 print_usage (stderr, 1);
	 case -1: /* done with options */
		 break;
	 default: /* something unexpected */
		 abort ();
      }
   } while (next_option != -1);
   /* END -options management */

   my_win_main();

   elm_run(); /* and run the program now  and handle all events etc. */

   elm_shutdown(); /* clean up and shut down */
   return 0;

}
